﻿// Genius
// Copyright (C) 2016 Dirk Friedenberger, www.frittenburger.de
//   
// This source file is subject to the Microsoft Public License (Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Genius.Brain.Shared.Strategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Genius.Brain.Shared.BO;
using Genius.Brain.Shared.Locator;
using System.IO;
using Newtonsoft.Json.Linq;
using Genius.Brain.Shared;

namespace Genius.Brain.Impl.Strategy
{
    class TextParserStrategy : ITextParserStrategy
    {
        private IJsonDatabase<ITokenDatabaseRecord> _database;

        public TextParserStrategy(string[] fileNames)
        {
            _database = BrainLocator.GetTokenDatabase();
            foreach(var fileName in fileNames)
                _database.LoadFile(fileName);
        }
        public IText Parse(string message)
        {
            var text = BrainLocator.GetText("#UNKNOWN","unknown");
            var mindiff = 1000; //Minimum Matching Diff Value
            var textMatcherStrategy = BrainLocator.GetTextMatcherStrategy();
            text.Original = message;
        
            foreach(var rec in _database.Data)
            {
                foreach (var p in rec.Pattern)
                {
                    var matchList = textMatcherStrategy.Match(p, message);
                    var diff = 0;
                    foreach (var match in matchList)
                    {
                        switch (match.MatchType)
                        {
                            case MatchType.Equal:
                            case MatchType.EqualIgnoreCase:
                            case MatchType.EqualIgnoreDiacritics:
                                break;

                            case MatchType.Extra:
                                switch (match.Word.Type)
                                {
                                    case "mark":
                                        break;
                                    default:
                                        diff += 500;
                                        break;
                                }
                                break;
                            case MatchType.Missing:
                                switch(match.Pattern.Type)
                                {
                                    case "mark":
                                        break;
                                    default:
                                        diff += 500;
                                        break;
                                }
                                break;
                            case MatchType.Different:
                            case MatchType.DifferentType:
                                diff += 1000;
                                break;
                            default:
                                throw new NotImplementedException(match.MatchType.ToString());
                        }

                    }
                    if (diff < mindiff)
                    {
                        mindiff = diff;
                        text.Token = rec.Token;
                        text.Language = rec.Language;
                    }
                
                }

            }


            return  text;
        }
    }
}
